//
//  SetUserInfoVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/14.
//

#import "SetUserInfoVC.h"
#import <BRPickerView.h>

@interface SetUserInfoVC ()
@property (nonatomic,strong) UIButton       * nextBtn;
@property (nonatomic,strong) UILabel        * nameLabel;
@property (nonatomic,strong) UILabel        * infoLabel;
@property (nonatomic,strong) UIImageView    * iconView;

@property (nonatomic,strong) CommonTipsView * sexView;
@property (nonatomic,strong) ReLayoutButton * boyBtn;
@property (nonatomic,strong) ReLayoutButton * grilBtn;

@property (nonatomic,strong) CommonTipsView * yearView;
@property (nonatomic,strong) UIView         * yearTapView;
@property (nonatomic,strong) UILabel        * yearLabel;
@property (nonatomic,strong) CommonTipsView * infoView;
@property (nonatomic,strong) UITextField        * heightLabel;
@property (nonatomic,strong) UITextField        * weightLabel;
@property (nonatomic,strong) UIButton       * enterBtn;

@property (nonatomic,strong) BRDatePickerView* dateView;
@property (nonatomic,strong) NSString * birthday;
@property (nonatomic,assign) NSInteger sex;

@end

@implementation SetUserInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navgationView hideInfo];
    [self setSubViewLayout];
}

-(void)setSubViewLayout{
    
//    [self.view addSubview:self.nextBtn];
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.infoLabel];
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.sexView];
    [self.view addSubview:self.boyBtn];
    [self.view addSubview:self.grilBtn];
    [self.view addSubview:self.yearView];
    [self.view addSubview:self.yearLabel];
    [self.view addSubview:self.yearTapView];
    [self.view addSubview:self.infoView];
    [self.view addSubview:self.heightLabel];
    [self.view addSubview:self.weightLabel];
    [self.view addSubview:self.enterBtn];
    
    [(BaseNavController *)[UIViewController findNavigationController]addFullScreenPopBlackListItem:self];

    self.sex = 1;
}

-(void)nextBtnAction{
    
    [(AppDelegate*) [UIApplication sharedApplication].delegate gotoApp];
    
}

-(void)grilBtnAction:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.boyBtn.selected = NO;
        sender.selected = YES;
        self.sex = 2;
    }
}

-(void)boyBtnAction:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.grilBtn.selected = NO;
        sender.selected = YES;
        self.sex = 1;
    }
}

-(void)yearTap{
    
    [self.dateView show];
}

-(void)enterBtnAction{
    
    if ([self.birthday isEqual:@""]) {
        FhudShowStr(@"请选择生日");
        return;
    }
    if ([self.heightLabel.text isEqual:@""]) {
        FhudShowStr(@"请输入身高");
        return;
    }
    if ([self.weightLabel.text isEqual:@""]) {
        FhudShowStr(@"请选择体重");
        return;
    }
    
    NSDictionary * parm = @{@"sex":@(self.sex),@"birthday":self.birthday,@"height":self.heightLabel.text ?: @"",@"weight":self.weightLabel.text ?: @""};
    [SPNetworkingIns POST:PORT(@"user-center/api/appUser/updateAppUserInfo") params:parm success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        FhudShowStr(@"设置成功");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [(AppDelegate*) [UIApplication sharedApplication].delegate gotoApp];
        });
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

-(UIButton *)nextBtn{
    
    if (!_nextBtn) {
        _nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH - 70 * FMScaleX, StatusBar, 60 * FMScaleX, 30 * FMScaleX)];
        [_nextBtn setTitle:@"跳过" forState:UIControlStateNormal];
        [_nextBtn setTitleColor:RDRGB(0xB6904A) forState:UIControlStateNormal];
        [_nextBtn addTarget:self action:@selector(nextBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _nextBtn.titleLabel.font = Bold(18);
    }
    return _nextBtn;
}

-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30 * FMScaleX, NavHeight + 12 * FMScaleX, 150 * FMScaleX, 45 * FMScaleX)];
        _nameLabel.textColor = RDRGB(0x714933);
        _nameLabel.font = Bold(32);
        _nameLabel.text = @"完善资料";
    }
    return _nameLabel;
}

-(UILabel *)infoLabel{
    
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(GETMinX(self.nameLabel), GETMaxY(self.nameLabel) + 5 * FMScaleX, 180 * FMScaleX, 20 * FMScaleX)];
        _infoLabel.font = Regular(14);
        _infoLabel.textColor = RDRGB(0x714933);
        _infoLabel.text = @"请完善您的身体基础数据";
    }
    return _infoLabel;
}

-(UIImageView *)iconView{
    
    if (!_iconView) {
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(20 * FMScaleX, GETMaxY(self.infoLabel) + 15 * FMScaleX, WIDTH - 40 * FMScaleX, 500 * FMScaleX)];
        _iconView.image = [UIImage imageNamed:@"Login_set_icon"];
    }
    return _iconView;
}

-(CommonTipsView *)sexView{
    
    if (!_sexView) {
        _sexView = [[CommonTipsView alloc]initWithFrame:CGRectMake((WIDTH  - 200 * FMScaleX) / 2, GETMinY(self.iconView) + 52 * FMScaleX, 200 * FMScaleX, 24 * FMScaleX)];
        [_sexView setInfoText:@"您的性别"];
    }
    return _sexView;
}

-(ReLayoutButton *)boyBtn{
    
    if(!_boyBtn ){
        _boyBtn = [[ReLayoutButton alloc]initWithFrame:CGRectMake(GETMinX(self.iconView) + 105 * FMScaleX, GETMaxY(self.sexView) + 20 * FMScaleX, 40 * FMScaleX, 60 * FMScaleX)];
        [_boyBtn setTitle:@"男" forState:UIControlStateNormal];
        [_boyBtn setTitleColor:RDRGB(0x999999) forState:UIControlStateNormal];
        [_boyBtn setTitleColor:RDRGB(0x9A7742 ) forState:UIControlStateSelected];

        [_boyBtn setImage:[UIImage imageNamed:@"Login_sex_boy"] forState:UIControlStateNormal];
        [_boyBtn setImage:[UIImage imageNamed:@"Login_sex_boy_sel"] forState:UIControlStateSelected];

        _boyBtn.imageView.frame = CGRectMake(0, 0, 13, 15);
        _boyBtn.layoutType = RelayoutTypeUpDown;
        _boyBtn.titleLabel.font = Medium(13);

        [_boyBtn addTarget:self action:@selector(boyBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _boyBtn.selected = YES;
    }
    return _boyBtn;
}

-(ReLayoutButton *)grilBtn{
    
    if(!_grilBtn ){
        _grilBtn = [[ReLayoutButton alloc]initWithFrame:CGRectMake(GETMaxX(self.boyBtn) + 40 * FMScaleX, GETMaxY(self.sexView) + 20 * FMScaleX, 40 * FMScaleX, 60 * FMScaleX)];
        [_grilBtn setTitle:@"女" forState:UIControlStateNormal];
        [_grilBtn setTitleColor:RDRGB(0x999999) forState:UIControlStateNormal];
        [_grilBtn setTitleColor:RDRGB(0x9A7742 ) forState:UIControlStateSelected];

        [_grilBtn setImage:[UIImage imageNamed:@"Login_sex_gril"] forState:UIControlStateNormal];
        [_grilBtn setImage:[UIImage imageNamed:@"Login_sex_gril_sel"] forState:UIControlStateSelected];

        _grilBtn.imageView.frame = CGRectMake(0, 0, 13, 15);
        _grilBtn.layoutType = RelayoutTypeUpDown;
        _grilBtn.titleLabel.font = Medium(13);

        [_grilBtn addTarget:self action:@selector(grilBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _grilBtn;
}

-(CommonTipsView *)yearView{
    
    if (!_yearView) {
        _yearView = [[CommonTipsView alloc]initWithFrame:CGRectMake((WIDTH  - 200 * FMScaleX) / 2, GETMaxY(self.boyBtn) + 18 * FMScaleX, 200 * FMScaleX, 24 * FMScaleX)];
        [_yearView setInfoText:@"您的生辰"];
    }
    return _yearView;
}


-(UILabel *)yearLabel{
    
    if (!_yearLabel) {
        _yearLabel = [[UILabel alloc]initWithFrame:CGRectMake((WIDTH  - 180 * FMScaleX) / 2, GETMaxY(self.yearView) + 26 * FMScaleX, 180 * FMScaleX, 28 * FMScaleX)];
        [_yearLabel setCornerRadius:4 * FMScaleX borderWidth:1 borderColor:RDRGB(0xF3EDE7)];
        _yearLabel.textColor = RDRGB(0x9A7742);
        _yearLabel.font = Medium(13);
        _yearLabel.textAlignment = NSTextAlignmentCenter;
        _yearLabel.text = @"请选择日期";
    }
    return _yearLabel;
}

-(UIView *)yearTapView{
    
    if (!_yearTapView) {
        _yearTapView = [[UIView alloc]initWithFrame:CGRectMake((WIDTH  - 180 * FMScaleX) / 2, GETMaxY(self.yearView) + 26 * FMScaleX, 180 * FMScaleX, 28 * FMScaleX)];
        _yearTapView.backgroundColor = UIColor.clearColor;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(yearTap)];
        [_yearTapView addGestureRecognizer:tap];
    }
    return _yearTapView;
}

-(CommonTipsView *)infoView{
    
    if (!_infoView) {
        _infoView = [[CommonTipsView alloc]initWithFrame:CGRectMake((WIDTH  - 200 * FMScaleX) / 2, GETMaxY(self.yearLabel) + 26 * FMScaleX, 200 * FMScaleX, 24 * FMScaleX)];
        [_infoView setInfoText:@"您的身高体重"];
    }
    return _infoView;
}

-(UITextField *)heightLabel{
    
    if (!_heightLabel) {
        _heightLabel = [[UITextField alloc]initWithFrame:CGRectMake(95 * FMScaleX, GETMaxY(self.infoView) + 26 * FMScaleX, 80 * FMScaleX, 28 * FMScaleX)];
        [_heightLabel setCornerRadius:4 * FMScaleX borderWidth:1 borderColor:RDRGB(0xF3EDE7)];
        _heightLabel.attributedPlaceholder = ({
            NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"CM" attributes:@{NSForegroundColorAttributeName:RDRGB(0x666666),NSFontAttributeName:Regular(14)}];
            string;
        });
        _heightLabel.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _heightLabel.textAlignment = NSTextAlignmentCenter;
        _heightLabel.textColor = RDRGB(0x333333);
    }
    return _heightLabel;
}

-(UITextField *)weightLabel{
    
    if (!_weightLabel) {
        _weightLabel = [[UITextField alloc]initWithFrame:CGRectMake(GETMaxX(self.heightLabel) + 20 * FMScaleX, GETMinY(self.heightLabel), 80 * FMScaleX, 28 * FMScaleX)];
        [_weightLabel setCornerRadius:4 * FMScaleX borderWidth:1 borderColor:RDRGB(0xF3EDE7)];
        _weightLabel.attributedPlaceholder = ({
            NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"KG" attributes:@{NSForegroundColorAttributeName:RDRGB(0x666666),NSFontAttributeName:Regular(14)}];
            string;
        });
        _weightLabel.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _weightLabel.textAlignment = NSTextAlignmentCenter;
        _weightLabel.textColor = RDRGB(0x333333);

    }
    return _weightLabel;
}

-(UIButton *)enterBtn{
    
    if (!_enterBtn) {
        _enterBtn = [[UIButton alloc]initWithFrame:CGRectMake((WIDTH - 125 * FMScaleX) / 2, GETMaxY(self.weightLabel) + 30 * FMScaleX, 125 * FMScaleX, 40 * FMScaleX)];
        [_enterBtn setBackgroundImage:[UIImage imageNamed:@"Login_info_btn"] forState:UIControlStateNormal];
        [_enterBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_enterBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_enterBtn addTarget:self action:@selector(enterBtnAction ) forControlEvents:UIControlEventTouchUpInside];
        _enterBtn.titleLabel.font = Bold(20);
    }
    return _enterBtn;
}

-(BRDatePickerView *)dateView{
    
    if (!_dateView) {
        _dateView =  [[BRDatePickerView alloc]init];
        _dateView.pickerMode = BRDatePickerModeYMD;
        _dateView.title = @"选择年月日";
        // datePickerView.selectValue = @"2019-10-30";
        _dateView.selectDate = [NSDate br_setYear:2019 month:10 day:30];
        _dateView.minDate = [NSDate br_setYear:1929 month:3 day:12];
        _dateView.maxDate = [NSDate date];
        @WeakObj(self)
        _dateView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
            @StrongObj(self)
            self.yearLabel.text = selectValue;
            self.birthday = selectValue;
        };
        // 设置自定义样式
        BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
        customStyle.pickerColor = UIColor.whiteColor;
        customStyle.pickerTextColor = RDRGB(0x9A7742);
        customStyle.separatorColor = RDRGB(0x9A7742);
        _dateView.pickerStyle = customStyle;
        }
    return _dateView;
}
@end
